$defs:
  # Схема для валидации самой JSONSchema
  $jsonschema:
    title: JSONSchema
    definitions:
      schemaArray:
        type: array
        minItems: 1
        items:
          "$ref": "#/$defs/$jsonschema"
      nonNegativeInteger:
        type: integer
        minimum: 0
      nonNegativeIntegerDefault0:
        allOf:
        - "$ref": "#/$defs/$jsonschema/definitions/nonNegativeInteger"
        - default: 0
      simpleTypes:
        enum:
        - array
        - boolean
        - integer
        - 'null'
        - number
        - object
        - string
      stringArray:
        type: array
        items:
          type: string
        uniqueItems: true
        default: []
    type:
    - object
    - boolean
    properties:
      "$id":
        type: string
        format: uri-reference
      "$schema":
        type: string
        format: uri
      "$ref":
        anyOf:
          - type: string
            format: uri-reference
          - $ref: "#/$defs/$defs"
      "$comment":
        type: string
      title:
        type: string
      description:
        type: string
      default: true
      readOnly:
        type: boolean
        default: false
      examples:
        type: array
        items: true
      multipleOf:
        type: number
        exclusiveMinimum: 0
      maximum:
        type: number
      exclusiveMaximum:
        type: number
      minimum:
        type: number
      exclusiveMinimum:
        type: number
      maxLength:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeInteger"
      minLength:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeIntegerDefault0"
      pattern:
        type: string
        format: regex
      additionalItems:
        "$ref": "#/$defs/$jsonschema"
      items:
        anyOf:
        - "$ref": "#/$defs/$jsonschema"
        - "$ref": "#/$defs/$jsonschema/definitions/schemaArray"
        default: true
      maxItems:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeInteger"
      minItems:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeIntegerDefault0"
      uniqueItems:
        type: boolean
        default: false
      contains:
        "$ref": "#/$defs/$jsonschema"
      maxProperties:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeInteger"
      minProperties:
        "$ref": "#/$defs/$jsonschema/definitions/nonNegativeIntegerDefault0"
      required:
        "$ref": "#/$defs/$jsonschema/definitions/stringArray"
      additionalProperties:
        "$ref": "#/$defs/$jsonschema"
      definitions:
        type: object
        additionalProperties:
          "$ref": "#/$defs/$jsonschema"
        default: {}
      properties:
        type: object
        additionalProperties:
          "$ref": "#/$defs/$jsonschema"
        default: {}
      patternProperties:
        type: object
        additionalProperties:
          "$ref": "#/$defs/$jsonschema"
        propertyNames:
          format: regex
        default: {}
      dependencies:
        type: object
        additionalProperties:
          anyOf:
          - "$ref": "#/$defs/$jsonschema"
          - "$ref": "#/$defs/$jsonschema/definitions/stringArray"
      propertyNames:
        "$ref": "#/$defs/$jsonschema"
      const: true
      enum:
        type: array
        items: true
        minItems: 1
        uniqueItems: true
      type:
        anyOf:
        - "$ref": "#/$defs/$jsonschema/definitions/simpleTypes"
        - type: array
          items:
            "$ref": "#/$defs/$jsonschema/definitions/simpleTypes"
          minItems: 1
          uniqueItems: true
      format:
        type: string
      contentMediaType:
        type: string
      contentEncoding:
        type: string
      if:
        "$ref": "#/$defs/$jsonschema"
      then:
        "$ref": "#/$defs/$jsonschema"
      else:
        "$ref": "#/$defs/$jsonschema"
      allOf:
        "$ref": "#/$defs/$jsonschema/definitions/schemaArray"
      anyOf:
        "$ref": "#/$defs/$jsonschema/definitions/schemaArray"
      oneOf:
        "$ref": "#/$defs/$jsonschema/definitions/schemaArray"
      not:
        "$ref": "#/$defs/$jsonschema"
    default: true
  $jsonata:
    title: JSONata запрос
    type: string
    pattern: (\s+|)\(((.*|\d|\D)+?)(\)(\s+|))$
  $menu-item:
    title: Элемент меню
    type: object
    properties:
      title:
        title: Название
        type: string
      location: 
        title: Размещение
        type: string
      icon: 
        title: Иконка
        type: string
      link:
        title: Ссылка
        type: string
    required:
      - title
      - location
  $menu:
    title: Меню
    type: array
    items:
      $ref: "#/$defs/$menu-item"
    minLength: 1
  $res-uri:
    title: URI ресурса
    type: string
    format: uri
  $res-uri-reference:
    title: URI-REFERENCE ресурса
    type: string
    format: uri-reference
  $data-source:
    title: Источник данных
    anyOf:
      - $ref: "#/$defs/$jsonata"
      - $ref: "#/$defs/$res-uri-reference"
      - title: Структура данных
        type:
          - object
          - array
      - $ref: "#/$defs/$dataset"
  $params:
    $ref: "#/$defs/$jsonschema"
  $source-profile:
    type: object
    properties:
      source:
        title: Источник данных / ресурс
        type: string
        $ref: "#/$defs/$data-source"
      origin:
        title: Базовый источник данных
        anyOf:
          - $ref: "#/$defs/$data-source"
            default: true
          - type: object
            patternProperties:
              ".*":
                $ref: "#/$defs/$data-source"
  $doc-profile:
    type: object
    $ref: "#/$defs/$source-profile"
    properties:
      type:
        title: Тип документа
        $ref: "#/$defs/$doc-types"
      template:
        title: Шаблон документа
        $ref: "#/$defs/$res-uri-reference"
# Схема базовых сущностей
$entities:
  functions:
    type: object
    additionalProperties: false
    patternProperties:
      "^[a-zA-Z0-9_]*(\\.[a-zA-Z0-9_]*)*$":
        title: Идентификатор сущности
        type: object
        properties:
          title: 
            type: string
            title: Описание функции
          params:
            type: array
            title: Параметры функции
            items:
              $ref: "#/$defs/$jsonschema"
          code:
            title: JSONata код функции
            type: string
          result:
            title: Схема результата
            $ref: "#/$defs/$jsonschema"
        required:
          - code

  entities:
    type: object
    additionalProperties: false
    patternProperties:
      "^[0-9a-zA-Z][a-zA-Z0-9_-]*(\\.[a-zA-Z][a-zA-Z0-9_-]*)*$":
        type: object
        properties:
          title:
            title: Название сущности
            type: string
          description: 
            title: Описание сущности
            type: string
          schema:
            $ref: "#/$defs/$jsonschema"
          menu:
            anyOf:
              - $ref: "#/$defs/$jsonata"
              - $ref: "#/$defs/$menu"
          presentations:
            type: object
            patternProperties:
              "^[0-9a-zA-Z][a-zA-Z0-9_-]*(\\.[a-zA-Z][a-zA-Z0-9_-]*)*$":
                $ref: "#/$defs/$doc-profile"
                properties:
                  title:
                    title: Название
                    type: string
                  params:
                    title: Схема параметров
                    $ref: "#/$defs/$params"
                  $constructor:
                    title: Конструктор презентации
                    $ref: "#/$defs/$jsonata"
                 







